package com.example.registerdemo.controller;

import com.example.registerdemo.common.UUIDUtils;
import com.example.registerdemo.entity.User;
import com.example.registerdemo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @RequestMapping("/registerUser")
    public String  register(User user) {
        user.setStatus(0);
        String name = user.getUsername();
        String code = UUIDUtils.getUUID();
        redisTemplate.opsForValue().set(name,code);
        userService.register(user);
        System.out.println(user);
        System.out.println("注册成功");
        return "login";
    }

    /**
     * 校验邮箱中的code激活账户
     * 首先根据激活码code查询用户，之后再把状态修改为"1"
     */
    @RequestMapping("/checkCode")
    public String checkCode(@Param("username") String username,@Param("code") String code) {
        System.out.println(username+","+code);
        User user = userService.checkCode(username);
        System.out.println("回调"+user);
        //如果用户不等于null，把用户状态修改status=1
        if (user != null) {
            //已经点击邮箱链接 将状态改为1
            user.setStatus(1);
            String c = (String) redisTemplate.opsForValue().get(username);
            if (!c.equals(code)){
                return "error";
            }
            //把code验证码清空，已经不需要了
            String vcode = UUIDUtils.getCode();

//            redisTemplate.opsForValue().set(name,vcode);

            System.out.println(user);
            userService.updateUserStatus(user);
        }
        return "login";
    }

    /**
     * 跳转到注册页面
     *
     * @return login
     */
    @RequestMapping("/indexPage")
    public String index() {
        return "register";
    }

    /**
     * 跳转到登录页面
     *
     * @return login
     */
    @RequestMapping("/loginPage")
    public String login() {
        return "login";
    }


    /**
     * 登录
     */
    @PostMapping("/loginUser")
    public String login(User user, Model model) {
        User u = userService.loginUser(user);
//        System.out.println(user.getVcode());
//        u.setVcode(user.getVcode());
//        String code = (String) redisTemplate.opsForValue().get(u.getUsername());
//        if (code==null) {
//            System.out.println("redis中验证码为空");
//            return "";
//        }
        if (u != null) {
//            if (user.getVcode().equals(code)){
                // 验证有效时间
//                Long expire = redisTemplate.opsForValue().getOperations().getExpire(code);
//                System.out.println("expire->>>" + expire);
//                if (expire < 0L) {
//                    model.addAttribute("msg", "验证码过期");
//                    return;
//                }
                //登录成功  跳转到welcome欢迎页
                System.out.println("登录成功");
                return "";
//            }else {
//                model.addAttribute("msg", "输入验证码不正确!");
//                return "";
//            }
        }else {
            model.addAttribute("msg", "用户名或密码有误，请确认是否已激活!");
            System.out.println("登陆失败");
            return "";
        }
    }

}
